package com.hs.mobile.controller;

import java.util.List;
import org.apache.log4j.Logger;

import com.hs.mobile.model.Device;
import com.hs.mobile.service.device.DeviceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;

@Controller
public class DeviceController {
	protected static Logger logger = Logger.getLogger("deviceController");
	
	@Resource(name="deviceService")
	private DeviceService deviceService;
	
	@Autowired
	public DeviceController( DeviceService deviceService )
	{
		this.deviceService = deviceService;
	}
	
	@GET
	@RequestMapping(value="/device/{id}", method=RequestMethod.GET)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody Device getDevice(@PathVariable("id") Long id)
	{
		logger.debug("Provider has received request to get device with id: " + id);		
		// Call service here
		return deviceService.findDeviceById(id);
	}
		
	// get total devices in database
	@GET
	@RequestMapping(value = "/devices",method = RequestMethod.GET)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody List<Device> getDevice() {
		logger.debug("Provider has received request to get all categories");
		// Call service here. access database
		List<Device> listDevices = null;
		listDevices = deviceService.getAllDevices();		
		return listDevices;
	}
		
	// get total available devices in database
	@GET
	@RequestMapping(value = "/availableDevices",method = RequestMethod.GET)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody List<Device> getAvailableDevice() {
		logger.debug("Provider has received request to get all categories");
		// Call service here. access database
		List<Device> listDevices = null;
		listDevices = deviceService.getAllAvailableDevices();		
		return listDevices;
	}
	
	//register device into database
	@POST
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody void addDevice(@RequestBody Device device) {
		 logger.debug("Provider has received request to add new device");
		 // Call service to here
		 deviceService.save(device);
	}
	
	//re-register device into database
	@PUT
	@RequestMapping(value = "/re-register", method = RequestMethod.PUT)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody void updateDevice(@RequestBody Device device) {
		 logger.debug("Provider has received request to add new device");
		 // Call service to here
		 deviceService.update(device);
	}
}
